package cn.itsource.system.controller;

import cn.itsource.system.service.IRoleService;
import cn.itsource.system.domain.Role;
import cn.itsource.system.query.RoleQuery;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    public IRoleService roleService;

    /**
     * 接口：添加或修改
     * @param role  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Role role){
        if( role.getId()!=null){
		roleService.update(role);
        }else{
                roleService.insert(role);
	}
        return new AjaxResult();

    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping("/delete/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        roleService.delete(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        roleService.patchDelete(ids);
        return new AjaxResult().setSuccess(true);
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/selectById/{id}")
    public AjaxResult findOne(@PathVariable("id")Long id) {
        return new AjaxResult().setResultObj(roleService.selectById(id));
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping("/selectAll")
    public AjaxResult findAll(){
        try {
            List<Role> roles = roleService.selectAll();
            return new AjaxResult().setSuccess(true).setResultObj(roles);

        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询失败");
        }

    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/selectByQueryPage")
    public AjaxResult queryPage(@RequestBody RoleQuery query) {
        return new AjaxResult().setResultObj(roleService.selectByQueryPage(query));
    }
}
